//
//  MyMessageTableViewCell.swift
//  Venus
//
//  Created by apple on 2018/8/5.
//  Copyright © 2018年 zhaowang. All rights reserved.
// 我的消息主界面cell

import UIKit

protocol  MyMessageTableViewCellDelegate : class {
    func reloadHeight(index:IndexPath)
}



class MyMessageTableViewCell: UITableViewCell {
    weak var delegate: MyMessageTableViewCellDelegate?
    /// 用户头像
    var userImaView : UIImageView = {
        let iv = UIImageView()
        return iv
    }()
    
    /// 用户昵称
    fileprivate lazy var nickNameLabel = UILabel(kColorC5, 14, "昵称")
    
    /// 日期
    fileprivate lazy var dateLabel = UILabel(kColorTextPlacehold, 12, "4月23日")
    
    /// 点赞图片
    fileprivate lazy var giveALikeIv : UIImageView = {
        let iv = UIImageView()
        return iv
    }()
    
    /// 评论内容
    fileprivate lazy var commentsLabel  : UILabel = {
        let lab = UILabel(kColorC5, 14)
        return lab
    }()
    
    /// 回复的标题
    fileprivate lazy var commentTitleLabel : UILabel = {
        let lab = UILabel(kColorC5, 12)
        return lab
    }()
    
    fileprivate lazy var lineView = UIView()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
          super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


// MARK: - 配置数据
extension MyMessageTableViewCell {
    func configModel(modelArr:[MyMessageModel] , index:IndexPath) {
        let model:MyMessageModel = modelArr[index.item]
        nickNameLabel.text = model.fromUser.userName
        userImaView.showImageS(model.fromUser.headImg)
        let time = String.stringToTimeStamp(stringTime: model.createTime)
        dateLabel.text = model.createTime.count != 0 ? time : "时间格式有误"
        commentsLabel.text = model.msg
        commentTitleLabel.text = model.replyMsg
        if model.msgType == 4 {
            if model.cellHeight == 0 {
                model.cellHeight = 110
                delegate?.reloadHeight(index: index)
            }
            commentsLabel.isHidden = true
            giveALikeIv.isHidden = false
        }else  {
            commentsLabel.isHidden = false
            giveALikeIv.isHidden = true
            if model.cellHeight == 0 {
                contentView.layoutIfNeeded()
                model.cellHeight = commentsLabel.frame.height + 80
                delegate?.reloadHeight(index: index)
            }
        }
    }
}

// MARK: - 配置UI
extension MyMessageTableViewCell{
    func setupUI() {
        selectionStyle = .none
        contentView.backgroundColor = UIColor.white
        contentView.addSubview(userImaView)
        contentView.addSubview(nickNameLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(giveALikeIv)
        contentView.addSubview(dateLabel)
        contentView.addSubview(commentsLabel)
        contentView.addSubview(commentTitleLabel)
        
        
        //头像
        userImaView.image = UIImage.init(named: "icon_pic")
        userImaView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(6)
            make.left.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 25, height: 25))
        }
        userImaView.fastCornerRadius(value: 12.5)
        
        //昵称
        nickNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userImaView.snp.right).offset(5)
            make.centerY.equalTo(userImaView.snp.centerY)
            make.height.equalTo(15)
        }
        
        //点赞
        giveALikeIv.image = UIImage.init(named: "icon_dianzan")
        giveALikeIv.snp.makeConstraints { (make) in
            make.left.equalTo(nickNameLabel)
            make.size.equalTo(CGSize(width: 20, height: 22))
            make.bottom.equalTo(nickNameLabel.snp.bottom).offset(50)
        }
        
        //日期
        dateLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().inset(10)
            make.left.equalTo(nickNameLabel)
        }
        
        
        //评论内容
        commentsLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nickNameLabel)
            make.top.equalTo(nickNameLabel.snp.bottom).offset(10)
            make.width.equalTo(LayoutCommon.width / 2 + 40)
        }
        commentsLabel.numberOfLines = 0
        
        //评论回复
        commentTitleLabel.numberOfLines = 3
        commentTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(commentsLabel)
            make.left.equalTo(commentsLabel.snp.right).offset(10)
            make.right.equalToSuperview().inset(10)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        lineView.backgroundColor = UIColor.init(hex:"eeeded")
    }
}
